package com.shiqing.aliface

import android.app.Activity
import android.content.Context
import androidx.annotation.NonNull
import com.aliyun.aliyunface.api.ZIMCallback
import com.aliyun.aliyunface.api.ZIMFacade
import com.aliyun.aliyunface.api.ZIMFacadeBuilder
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** AlifacePlugin */
class AlifacePlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private lateinit var channel: MethodChannel

    private lateinit var mContext: Context

    private var mActivity: Activity? = null


    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, "aliface")
        channel.setMethodCallHandler(this)
        mContext = flutterPluginBinding.applicationContext

    }

    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        when (call.method) {
            // 初始化阿里实人认证库
            "registerFace" -> {
                ZIMFacade.install(mContext)
                val params = hashMapOf<String, Any>()
                params["code"] = true
                params["msg"] = "初始化成功"
                result.success(params)
            }
            "getMetaInfo" -> {
                val metaInfo = ZIMFacade.getMetaInfos(mContext)
                val params = hashMapOf<String, Any>().also {
                    it["metaInfo"] = metaInfo
                }
                result.success(params)
            }
            // 开始实人认证
            "startVerifier" -> {
                val certifyId = call.argument<String>("certifyId")
                val extParams = hashMapOf<String, String>().also {
                    it[ZIMFacade.ZIM_EXT_PARAMS_KEY_SCREEN_ORIENTATION] =
                        ZIMFacade.ZIM_EXT_PARAMS_VAL_SCREEN_PORT
                }
                ZIMFacadeBuilder.create(mActivity)
                    .verify(certifyId, true, extParams, ZIMCallback { response ->
                        val params = hashMapOf<String, Any>().also {
                            it["code"] = response.code
                            it["msg"] = if (response.msg == null) "" else response.msg
                        }
                        result.success(params)
                        return@ZIMCallback true
                    })
            }
        }
    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }

    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        mActivity = binding.activity
    }

    override fun onDetachedFromActivityForConfigChanges() {
    }

    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
    }

    override fun onDetachedFromActivity() {
        mActivity = null
    }
}
